//
//  MineViewController.m
//  RoadLine
//
//  Created by dllo on 15/4/27.
//  Copyright (c) 2015年 RedFireNoodles. All rights reserved.
//

#import "MineViewController.h"
#import "NightManager.h"
#import "Singleton.h"
#import "MineCell.h"
#import <ShareSDK/ShareSDK.h>
#import "SDImageCache.h"
#import "aboutUsViewController.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height


@interface MineViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, retain)NSMutableArray *picArr;

@end

@implementation MineViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray arrayWithObjects:@"分享",@"清除缓存",@"夜间模式",@"关于我们", nil];
        self.picArr = [NSMutableArray arrayWithObjects:@"iconfont-forwardfill.png",@"3.png",@"iconfont-xingguang.png",@"women.png",@"iconfont-likefill.png", nil];
    }

    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"colorPicture"]]];
    self.title = @"我的";
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:tableView];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.separatorStyle = NO;
    [tableView release];
//    tableView.backgroundColor = [UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1];
    tableView.tag = 1000;
    tableView.backgroundColor = [UIColor clearColor];
    
    //修改uinavigationBar的字体颜色为白色
    UILabel *titleLabelPrice = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    titleLabelPrice.font = [UIFont boldSystemFontOfSize:18];
    titleLabelPrice.textColor = [UIColor whiteColor];
    titleLabelPrice.backgroundColor = [UIColor clearColor];
    titleLabelPrice.text = @"      我的";
    self.navigationItem.titleView = titleLabelPrice;
    [titleLabelPrice release];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 1;

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 4;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 40;

}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

  return @" ";

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *reuse = @"reuse";
    MineCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[MineCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse]autorelease];
    }
    if (indexPath.section == 2) {
        if ([Singleton shareSingleton].isNight) {
            cell.label.text = @"白天模式";
            cell.label.backgroundColor = [UIColor clearColor];
            cell.image.image = [UIImage imageNamed:@"iconfont-qing"];
            cell.contentView.backgroundColor = [UIColor clearColor];
        }else{
            cell.label.text = @"夜间模式";
            cell.label.backgroundColor = [UIColor clearColor];
            cell.contentView.backgroundColor = [UIColor clearColor];
            cell.image.image = [UIImage imageNamed:@"iconfont-xingguang"];
        }
    } else if (indexPath.section == 1) {
        NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *sandBoxPath = [sandBox lastObject];
        CGFloat size = [self folderSizeAtPath:sandBoxPath];
        cell.image.image = [UIImage imageNamed:@"3.png"];
        cell.label.text = [NSString stringWithFormat:@"清理缓存(%.2fM)", size];
//        cell.label.textColor = [UIColor whiteColor];
        cell.selectionStyle = NO;
    } else{
        cell.label.text = self.arr[indexPath.section];
        cell.image.image = [UIImage imageNamed:self.picArr[indexPath.section]];
    }
    cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:cell.frame] autorelease];
    cell.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1];
    cell.backgroundColor = [UIColor colorWithRed:249/255.0 green:242/255.0 blue:226/255.0 alpha:0.5];
    return cell;
}
// 清除缓存
- (void)clearCache:(NSString *)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles = [fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }
}
// 计算单个文件大小
- (CGFloat)fileSizeAtPath:(NSString *)path {

    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        long long size = [fileManager attributesOfItemAtPath:path error:nil].fileSize;
        return size / 1024.0 / 1024.0;
    }
    return 0;
}
// 计算目录大小
- (CGFloat)folderSizeAtPath:(NSString *)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    float folderSize;
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles = [fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            folderSize += [self fileSizeAtPath:absolutePath];
        }
    }
    return folderSize;
}
// 点击alert上的按钮执行的方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *sandBoxPath = [sandBox lastObject];
        [self clearCache:sandBoxPath];
        UITableView *tblView = (UITableView *)[self.view viewWithTag:1000];
        [tblView reloadData];
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"确认清除缓存么？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
        [alertView show];
        [alertView release];
    }
    else if (indexPath.section == 2){
        Singleton *single = [Singleton shareSingleton];
        single.isNight = !single.isNight;
        [single saveNightModeInfoToDisk];
        [tableView reloadData];
      
    } else if (indexPath.section == 3){
        aboutUsViewController *aboutUsVC = [[aboutUsViewController alloc]init];
        [self.navigationController pushViewController:aboutUsVC animated:YES];
    } else if (indexPath.section == 0){
        
        // 分享
        NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"ShareSDK" ofType:@"png"];
        id<ISSContent> publishContent = [ShareSDK content:@"旅行出行没路线? 看看别人都是怎么玩儿的! 攻略详情尽在@途行 来人啊,把攻略端上来.https://appsto.re/cn/F_4k7.i"
                                           defaultContent:@"欢迎使用途行旅行"
                                                    image:[ShareSDK imageWithPath:imagePath]
                                                    title:@"途行"
                                                      url:@"http://www.baidu.com"
                                              description:@"这是一条测试信息"
                                                mediaType:SSPublishContentMediaTypeNews];
        //创建弹出菜单容器
        id<ISSContainer> container = [ShareSDK container];
//        [container setIPadContainerWithView:sender arrowDirect:UIPopoverArrowDirectionUp];
        
        //弹出分享菜单
        [ShareSDK showShareActionSheet:container
                             shareList:nil
                               content:publishContent
                         statusBarTips:YES
                           authOptions:nil
                          shareOptions:nil
                                result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                                    
                                    if (state == SSResponseStateSuccess)
                                    {
                                        NSLog(NSLocalizedString(@"TEXT_ShARE_SUC", @"分享成功"));
                                    }
                                    else if (state == SSResponseStateFail)
                                    {
                                        NSLog(NSLocalizedString(@"TEXT_ShARE_FAI", @"分享失败,错误码:%d,错误描述:%@"), [error errorCode], [error errorDescription]);
                                    }
                                }];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
    sectionView.backgroundColor = [UIColor clearColor];
    return sectionView;
}

- (void)setLightDayModel {
    UITableView *table = (UITableView *)[self.view viewWithTag:1000];
//    table.backgroundColor = [UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1];
    table.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"colorPicture"]];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:246/255.0 green:85/255.0 blue:91/255.0 alpha:1];
    
}

- (void)setNightDayModel {
    UITableView *table = (UITableView *)[self.view viewWithTag:1000];
    table.backgroundColor = [UIColor blackColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:4/255.0 green:165/255.0 blue:171/255.0 alpha:1];

}
- (void)dealloc
{
    [_arr release];
    [_picArr release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
